﻿(function ($) {

    var _dataMigrationService = abp.services.app.dataMigration;
    var _$modal = $('#DataEditModal');
    var _$form = $('form[name=DataEditForm]');

    function save() {

        if (!_$form.valid()) {
            return;
        }

        var data = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js
        //上传文件
        var resourceIds = resourceUpload();
        data.ResourceIds = resourceIds;

        abp.ui.setBusy(_$form);
        _dataMigrationService.updateData(data).done(function () {
            _$modal.modal('hide');
            location.reload(true); //reload page to see edited user!
        }).always(function () {
            abp.ui.clearBusy(_$modal);
        });
    }

    //Handle save button click
    _$form.closest('div.modal-content').find(".save-button").click(function (e) {
        e.preventDefault();
        save();
    });

    //Handle enter key
    _$form.find('input').on('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            save();
        }
    });

    $.AdminBSB.input.activate(_$form);

    _$modal.on('shown.bs.modal', function () {
        _$form.find('input[type=text]:first').focus();
    });

    //资源操作


    _$form.find('.add-resource').click(function () {
        var template = _$form.find('.add-resource-template').html();
        var $container = _$form.find('.add-resource-container');
        var $item = $(template);
        $item.find('.btn-select-resource').on('click', function () {
            $item.find('input[type="file"]').on('change', function () {
                $item.find('input[type="text"]').val($(this).val());
            }).click();
        });
        $item.find('.btn-remove-resource').on('click', function () {
            var _$item = $item;
            _$item.remove();
        });
        $container.append($item);
    });

    function resourceUpload() {
        var $inputs = _$form.find('.add-resource-container input[type="file"]');
        var formData = new FormData();
        $inputs.each(function () {
            formData.append('resource-' + new Date().getTime(), this.files[0]);
        });
        var ids = [];
        $.ajax({
            type: 'post'
            , url: abp.appPath + 'api/services/app/Resources/Post'
            , cache: false
            , dataType: 'JSON'
            , processData: false
            , contentType: false
            , data: formData
            , async: false
        }).done(function (res) {
            console.log(res);
            $.each(res.result.resources, function () {
                ids.push(this.id);
            });
        });
        return ids;
    }

    //删除资源
    $('.btn-remove-resource').on('click', function () {
        var $this = $(this);
        var feedbackresourceId = $(this).attr('data-dataresource-id');
        var resourceName = $(this).attr('data-resource-name');
        abp.message.confirm(
            abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'DevM'), resourceName),
            function (isConfirmed) {
                if (isConfirmed) {
                    _dataMigrationService.deleteDataResource({ id: feedbackresourceId }).done(function () {
                        while (!$this.hasClass('list-group-item')) {
                            $this = $this.parent();
                        }
                        $this.remove();
                    });
                }
            }
        );
    });

})(jQuery);